/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package dp1.titandevelop.titano.view.sec;

import dp1.titandevelop.titano.service.AuditoriaService;
import dp1.titandevelop.titano.view.CambiarPassword;
import dp1.titandevelop.titano.view.LoginT;
import java.awt.Cursor;
import java.awt.Dimension;
import java.awt.FileDialog;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JOptionPane;

/**
 *
 * @author christianjuarez
 */
public class ModulosT_SEG extends javax.swing.JFrame {

    public static ModulosT_SEG_AUsu admUser = null;

    /**
     * Creates new form ModulosTSEG
     */
    private BufferedImage imgFondo;
    public ModulosT_SEG() {
        initComponents();

        this.setLocationRelativeTo(null);
        this.setExtendedState(java.awt.Frame.MAXIMIZED_BOTH);
     }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        try {
            //this.getClass().getResource("/img/fondo.png").toURI();
            this.imgFondo = ImageIO.read(getClass().getResourceAsStream("/img/fondoTitanBright.png"));
        } catch (IOException ex) {
            ex.printStackTrace();
        }
        escritorioSeguridad = new javax.swing.JDesktopPane(){
            @Override
            protected void paintComponent(Graphics g) {
                super.paintComponent(g);
                g.drawImage(imgFondo, 0, 0, this);
            }

            @Override
            public Dimension getPreferredSize() {
                return new Dimension(imgFondo.getWidth(), imgFondo.getHeight());
            }

        };
        jPanel2 = new javax.swing.JPanel();
        lblUsuario = new javax.swing.JLabel();
        lblUsuarioLogeado = new javax.swing.JLabel();
        lblCorreo = new javax.swing.JLabel();
        lblCorreoLogeado = new javax.swing.JLabel();
        btnCambiarPW = new javax.swing.JButton();
        lblPerfil = new javax.swing.JLabel();
        lblPerfilLogeado = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jMenuBar1 = new javax.swing.JMenuBar();
        jMenu1 = new javax.swing.JMenu();
        menuUsuario = new javax.swing.JMenuItem();
        menuPerfil = new javax.swing.JMenuItem();
        jMenuItem1 = new javax.swing.JMenuItem();

        setTitle("Módulo Seguridad");

        escritorioSeguridad.setPreferredSize(null);

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder(""));

        lblUsuario.setText("Usuario:");

        lblUsuarioLogeado.setText(LoginT.empleadoLogin.getNombres()+" "+LoginT.empleadoLogin.getApellidop()+" "+LoginT.empleadoLogin.getApellidom()+" "+"("+LoginT.userLogin.getUsuario()+")");

        lblCorreo.setText("Correo:");

        lblCorreoLogeado.setText(LoginT.empleadoLogin.getEmail());

        btnCambiarPW.setText("Cambiar Password");
        btnCambiarPW.setBorder(javax.swing.BorderFactory.createTitledBorder(""));
        btnCambiarPW.setPreferredSize(new java.awt.Dimension(121, 20));
        btnCambiarPW.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCambiarPWActionPerformed(evt);
            }
        });

        lblPerfil.setText("Perfil:");

        lblPerfilLogeado.setText(LoginT.perfilLogin.getDescripcion());

        jLabel2.setIcon(new javax.swing.ImageIcon(getClass().getResource("/img/usuario.png"))); // NOI18N

        jLabel4.setIcon(new javax.swing.ImageIcon(getClass().getResource("/img/perfilesactivos.png"))); // NOI18N

        jLabel5.setIcon(new javax.swing.ImageIcon(getClass().getResource("/img/correo.png"))); // NOI18N

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(lblUsuario)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(lblUsuarioLogeado)
                .addGap(27, 27, 27)
                .addComponent(jLabel5)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(lblCorreo)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(lblCorreoLogeado)
                .addGap(32, 32, 32)
                .addComponent(jLabel4)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(lblPerfil)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(lblPerfilLogeado)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(btnCambiarPW, javax.swing.GroupLayout.PREFERRED_SIZE, 131, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                .addGap(0, 0, Short.MAX_VALUE)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblUsuario)
                    .addComponent(lblUsuarioLogeado)
                    .addComponent(lblCorreo)
                    .addComponent(lblCorreoLogeado)
                    .addComponent(btnCambiarPW, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(lblPerfil)
                    .addComponent(lblPerfilLogeado)
                    .addComponent(jLabel2)
                    .addComponent(jLabel4)
                    .addComponent(jLabel5)))
        );

        jMenu1.setText("Administrar");

        menuUsuario.setText("Usuario");
        menuUsuario.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                menuUsuarioActionPerformed(evt);
            }
        });
        jMenu1.add(menuUsuario);

        menuPerfil.setText("Perfil");
        menuPerfil.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                menuPerfilActionPerformed(evt);
            }
        });
        jMenu1.add(menuPerfil);

        jMenuItem1.setText("Auditoria");
        jMenuItem1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem1ActionPerformed(evt);
            }
        });
        jMenu1.add(jMenuItem1);

        jMenuBar1.add(jMenu1);

        setJMenuBar(jMenuBar1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(escritorioSeguridad, javax.swing.GroupLayout.DEFAULT_SIZE, 829, Short.MAX_VALUE)
                .addContainerGap())
            .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(escritorioSeguridad, javax.swing.GroupLayout.DEFAULT_SIZE, 810, Short.MAX_VALUE)
                .addGap(10, 10, 10)
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void menuUsuarioActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_menuUsuarioActionPerformed
        //ID 7 Administrar usuario
        //NO MODIFICAR !!!!!!!!!
        Integer i = 0;
        for (; i < LoginT.permisosxperfil.size(); i++) {
            if (7 == (Integer) LoginT.idspermisosxperfil.get(i)) {

                admUser = new ModulosT_SEG_AUsu();
                this.escritorioSeguridad.add(admUser);
                Dimension desktopSize = this.getSize();
                Dimension jInternalFrameSize = admUser.getSize();
                admUser.setLocation((desktopSize.width - jInternalFrameSize.width) / 2,
                        (desktopSize.height - jInternalFrameSize.height) / 4);
                admUser.show();
                break;
            }

        }
        if (i >= LoginT.permisosxperfil.size()) {
            JOptionPane.showMessageDialog(this.escritorioSeguridad, "No puedes proceder, al parecer careces de permisos.",
                    "No posee permisos",
                    JOptionPane.WARNING_MESSAGE);
        }
    }//GEN-LAST:event_menuUsuarioActionPerformed

    private void menuPerfilActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_menuPerfilActionPerformed
        //ID 8
        //NO MODIFICAR !!!!!!!!!
        Integer i = 0;
        for (; i < LoginT.permisosxperfil.size(); i++) {
            if (8 == (Integer) LoginT.idspermisosxperfil.get(i)) {

                ModulosT_SEG_APerfil admPermiso = new ModulosT_SEG_APerfil();
                this.escritorioSeguridad.add(admPermiso);
                Dimension desktopSize = this.getSize();
                Dimension jInternalFrameSize = admPermiso.getSize();
                admPermiso.setLocation((desktopSize.width - jInternalFrameSize.width) / 2,
                        (desktopSize.height - jInternalFrameSize.height) / 4);
                admPermiso.show();
                break;
            }

        }
        if (i >= LoginT.permisosxperfil.size()) {
            JOptionPane.showMessageDialog(this.escritorioSeguridad, "No puedes proceder, al parecer careces de permisos.",
                    "No posee permisos",
                    JOptionPane.WARNING_MESSAGE);
        }

    }//GEN-LAST:event_menuPerfilActionPerformed

    private void btnCambiarPWActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCambiarPWActionPerformed

        CambiarPassword chngpw = new CambiarPassword(new javax.swing.JFrame(), true);
        chngpw.setLocationRelativeTo(null);
        chngpw.setVisible(true);
    }//GEN-LAST:event_btnCambiarPWActionPerformed

    private void jMenuItem1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem1ActionPerformed
     
        ArrayList<String> username = new  ArrayList<String>();
        ArrayList<String> perfiles = new  ArrayList<String>();
        ArrayList<String> tablas = new  ArrayList<String>();
        ArrayList<String> acciones = new  ArrayList<String>();
        ArrayList<String> fechas = new  ArrayList<String>();
        AuditoriaService as = new AuditoriaService();
        System.out.println("Llegue hasta aqui");
        as.leerDatosAuditoria(username,perfiles,tablas,acciones,fechas);
        
        try {
            
            if(!username.isEmpty() || !perfiles.isEmpty() || !tablas.isEmpty() || !acciones.isEmpty() || !fechas.isEmpty()  ){
            System.out.println("entre aqui");
                String nombreArchivo = "/tmp/auditoria.pdf";
            FileDialog fd = new FileDialog(new JFrame(), "Guardar", FileDialog.SAVE);
            fd.setDirectory(".");
            fd.setFile("*.pdf");
            fd.setVisible(true);
            nombreArchivo = fd.getDirectory() + fd.getFile();
            
              
              if(fd.getFile()!=null){
                  this.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
                  reporte_auditoria report=new reporte_auditoria(fechas,username,perfiles,tablas,acciones,nombreArchivo);
                  this.setCursor(Cursor.getDefaultCursor());
                  JOptionPane.showInternalMessageDialog(this.getRootPane(),"El Reporte a sido generado satisfactoriamente","Mensaje",JOptionPane.INFORMATION_MESSAGE);
              }
            }else{
            JOptionPane.showInternalMessageDialog(this.getRootPane(),"No hay datos para ingresar al reporte", "Aviso", JOptionPane.WARNING_MESSAGE);
            }
        } catch (Exception ex) {
            Logger.getLogger(ModulosT_SEG_Audit.class.getName()).log(Level.SEVERE, null, ex);
        }
         
    }//GEN-LAST:event_jMenuItem1ActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(ModulosT_SEG.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(ModulosT_SEG.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(ModulosT_SEG.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(ModulosT_SEG.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new ModulosT_SEG().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnCambiarPW;
    private javax.swing.JDesktopPane escritorioSeguridad;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JMenu jMenu1;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JMenuItem jMenuItem1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JLabel lblCorreo;
    private javax.swing.JLabel lblCorreoLogeado;
    private javax.swing.JLabel lblPerfil;
    private javax.swing.JLabel lblPerfilLogeado;
    private javax.swing.JLabel lblUsuario;
    private javax.swing.JLabel lblUsuarioLogeado;
    private javax.swing.JMenuItem menuPerfil;
    private javax.swing.JMenuItem menuUsuario;
    // End of variables declaration//GEN-END:variables
}
